package com.lyl.serviceorder.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyl.commonutils.JwtUtils;
import com.lyl.commonutils.R;
import com.lyl.exception.ZxjyException;
import com.lyl.serviceorder.entity.Order;
import com.lyl.serviceorder.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-25
 */
@RestController
@RequestMapping("/serviceorder/api/order")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    @PutMapping("/removeOrder/{oid}")
    R removeOrder(@PathVariable("oid") Long oid) {

        boolean b = orderService.removeById(oid);
        return R.ok();
    }
    @ApiOperation("根据会员id与课程id判断是否购买此课程")
    @GetMapping("isBuyCourse/{memberId}/{courseId}")
    public R isBuyCourse(@PathVariable("memberId") Long memberId,
                               @PathVariable("courseId") Long courseId) {
        System.out.println("课程id:"+courseId+"-->"+memberId);
        //订单状态是1表示支付成功
        int count = orderService.count(new QueryWrapper<Order>().eq("member_id", memberId).eq("course_id", courseId).eq("status", 1));
        if(count>0) {
            return R.ok();// 购买了
        } else {
            return R.error();//没有购买
        }
    }

    /**
     * 根据订单编号 获取订单
     * @param orderNo
     * @return
     */
    @GetMapping("getOrder/{orderNo}")
    public R get(@PathVariable("orderNo") String orderNo) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

    /**
     * 根据用户id 获取所有购物车里面的东西, 未支付订单 属于购物车
     * @return
     */
    @GetMapping("getCart")
    public R getAllCart(
                         HttpServletRequest request) {
        System.out.println("token:"+request.getHeader("token") );
//        Long member_id=Long.parseLong( JwtUtils.getMemberIdByJwtToken(request,"id"));
        //
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",1541347257739563009L);
        wrapper.eq("status",0);// 未支付的
        wrapper.orderByDesc(true,"gmt_modified");
        List<Order> orders= orderService.selectList(wrapper);

        return R.ok().data("item", orders);
    }

    /**
     * 根据用户id 获取所有购物车里面的东西, 未支付订单 属于购物车
     * @return
     */
    @GetMapping("getOrder")
    public R getAllOrder(
            HttpServletRequest request) {
        Long member_id=Long.parseLong( JwtUtils.getMemberIdByJwtToken(request,"id"));
        //
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",member_id);
        wrapper.eq("status",1);// 已支付的就是订单
        wrapper.orderByDesc(true,"gmt_modified");

        List<Order> orders= orderService.selectList(wrapper);

        return R.ok().data("item", orders);
    }

    //根据课程id和用户id创建订单，返回订单id
    @PostMapping("createOrder/{courseId}")
    public R save(@PathVariable("courseId") Long courseId, HttpServletRequest request) {
        // 如果没有登录
        if(request.getHeader("token")==null){
            throw new ZxjyException(28004,"请先登录");
        }
        //1540144064972066817
        //
        String orderId = orderService.saveOrder(courseId, Long.parseLong(JwtUtils.getMemberIdByJwtToken(request,"id")));
        return R.ok().data("orderId", orderId);
    }

    //根据课程id和用户id创建订单，返回订单id
    @PostMapping("createOrderResume/{resumeModelId}")
    public R createOrderResume(@PathVariable("resumeModelId") Long resumeModelId, HttpServletRequest request) {
        // 如果没有登录
        if(request.getHeader("token")==null){
            throw new ZxjyException(28004,"请先登录");
        }
        //1540144064972066817
        //
        String orderId = orderService.saveOrderResume(resumeModelId, Long.parseLong(JwtUtils.getMemberIdByJwtToken(request,"id")));
        return R.ok().data("orderId", orderId);
    }
}

